<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src/lib/plugin.coffee</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="icon" href="../assets/favicon.ico">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
                <h1><img src="../assets/css/logo.png" title="" width="117" height="52"></h1>
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: </em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
                    <h2 class="off-left">APIs</h2>
                    <div id="api-tabview" class="tabview">
                        <ul class="tabs">
                            <li><a href="#api-classes">Classes</a></li>
                            <li><a href="#api-modules">Modules</a></li>
                        </ul>
                
                        <div id="api-tabview-filter">
                            <input type="search" id="api-filter" placeholder="Type to filter APIs">
                        </div>
                
                        <div id="api-tabview-panel">
                            <ul id="api-classes" class="apis classes">
                                <li><a href="../classes/BasePlugin.html">BasePlugin</a></li>
                                <li><a href="../classes/Collection.html">Collection</a></li>
                                <li><a href="../classes/Docpad.html">Docpad</a></li>
                                <li><a href="../classes/docpadUtil.html">docpadUtil</a></li>
                                <li><a href="../classes/DocumentModel.html">DocumentModel</a></li>
                                <li><a href="../classes/ElementsCollection.html">ElementsCollection</a></li>
                                <li><a href="../classes/Events.html">Events</a></li>
                                <li><a href="../classes/FileModel.html">FileModel</a></li>
                                <li><a href="../classes/FilesCollection.html">FilesCollection</a></li>
                                <li><a href="../classes/MetaCollection.html">MetaCollection</a></li>
                                <li><a href="../classes/Model.html">Model</a></li>
                                <li><a href="../classes/PluginLoader.html">PluginLoader</a></li>
                                <li><a href="../classes/PluginTester.html">PluginTester</a></li>
                                <li><a href="../classes/QueryCollection.html">QueryCollection</a></li>
                                <li><a href="../classes/ScriptCollection.html">ScriptCollection</a></li>
                                <li><a href="../classes/ServerTester.html">ServerTester</a></li>
                                <li><a href="../classes/StylesCollection.html">StylesCollection</a></li>
                            </ul>
                
                
                            <ul id="api-modules" class="apis modules">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
                    Show:
                    <label for="api-show-inherited">
                        <input type="checkbox" id="api-show-inherited" checked>
                        Inherited
                    </label>
            
                    <label for="api-show-protected">
                        <input type="checkbox" id="api-show-protected">
                        Protected
                    </label>
            
                    <label for="api-show-private">
                        <input type="checkbox" id="api-show-private">
                        Private
                    </label>
                    <label for="api-show-deprecated">
                        <input type="checkbox" id="api-show-deprecated">
                        Deprecated
                    </label>
            
                </div>
            
            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
<h1 class="file-heading">File: src/lib/plugin.coffee</h1>

<div class="file">
    <pre class="code prettyprint linenums">
# ---------------------------------
# Requires

# External
extendr = require(&#x27;extendr&#x27;)
typeChecker = require(&#x27;typechecker&#x27;)
ambi = require(&#x27;ambi&#x27;)
eachr = require(&#x27;eachr&#x27;)


# ---------------------------------
# Classes

# Define Plugin
###*
# The base class for all DocPad plugins
# @class BasePlugin
# @constructor
###
class BasePlugin

	###*
	# Add support for BasePlugin.extend(proto)
	# @private
	# @property {Object} @extend
	###
	@extend: require(&#x27;csextends&#x27;)

	# ---------------------------------
	# Inherited

	###*
	# The DocPad Instance
	# @private
	# @property {Object} docpad
	###
	docpad: null

	# ---------------------------------
	# Variables

	###*
	# The plugin name
	# @property {String}
	###
	name: null

	###*
	# The plugin config
	# @property {Object}
	###
	config: {}
	
	###*
	# The instance config.
	# @property {Object}
	###
	instanceConfig: {}

	###*
	# Plugin priority
	# @private
	# @property {Number}
	###
	priority: 500

	###*
	# Constructor method for the plugin
	# @method constructor
	# @param {Object} opts
	###
	constructor: (opts) -&gt;
		# Prepare
		me = @
		{docpad,config} = opts
		@docpad = docpad

		# Bind listeners
		@bindListeners()

		# Swap out our configuration
		@config = extendr.deepClone(@config)
		@instanceConfig = extendr.deepClone(@instanceConfig)
		@initialConfig = @config
		@setConfig(config)

		# Return early if we are disabled
		return @  if @isEnabled() is false

		# Listen to events
		@addListeners()

		# Chain
		@

	###*
	# Set Instance Configuration
	# @private
	# @method setInstanceConfig
	# @param {Object} instanceConfig
	###
	setInstanceConfig: (instanceConfig) -&gt;
		# Merge in the instance configurations
		if instanceConfig
			extendr.safeDeepExtendPlainObjects(@instanceConfig, instanceConfig)
			extendr.safeDeepExtendPlainObjects(@config, instanceConfig)  if @config
		@

	###*
	# Set Configuration
	# @private
	# @method {Object} setConfig
	# @param {Object} [instanceConfig=null]
	###
	setConfig: (instanceConfig=null) =&gt;
		# Prepare
		docpad = @docpad
		userConfig = @docpad.config.plugins[@name]
		@config = @docpad.config.plugins[@name] = {}

		# Instance config
		@setInstanceConfig(instanceConfig)  if instanceConfig

		# Merge configurations
		configPackages = [@initialConfig, userConfig, @instanceConfig]
		configsToMerge = [@config]
		docpad.mergeConfigurations(configPackages, configsToMerge)

		# Remove listeners if we are disabled
		@removeListeners()  unless @isEnabled()

		# Chain
		@

	###*
	# Get the Configuration
	# @private
	# @method {Object}
	###
	getConfig: =&gt;
		return @config

	###*
	# Alias for b/c
	# @private
	# @method bindEvents
	###
	bindEvents: -&gt; @addListeners()


	###*
	# Bind Listeners
	# @private
	# @method bindListeners
	###
	bindListeners: -&gt;
		# Prepare
		pluginInstance = @
		docpad = @docpad
		events = docpad.getEvents()

		# Bind events
		eachr events, (eventName) -&gt;
			# Fetch the event handler
			eventHandler = pluginInstance[eventName]

			# Check it exists and is a function
			if typeChecker.isFunction(eventHandler)
				# Bind the listener to the plugin
				pluginInstance[eventName] = eventHandler.bind(pluginInstance)

		# Chain
		@


	###*
	# Add Listeners
	# @private
	# @method addListeners
	###
	addListeners: -&gt;
		# Prepare
		pluginInstance = @
		docpad = @docpad
		events = docpad.getEvents()

		# Bind events
		eachr events, (eventName) -&gt;
			# Fetch the event handler
			eventHandler = pluginInstance[eventName]

			# Check it exists and is a function
			if typeChecker.isFunction(eventHandler)
				# Apply the priority
				eventHandlerPriority = pluginInstance[eventName+&#x27;Priority&#x27;] or pluginInstance.priority or null
				eventHandler.priority ?= eventHandlerPriority
				eventHandler.name = &quot;#{pluginInstance.name}: {eventName}&quot;
				eventHandler.name += &quot;(priority eventHandler.priority})&quot;  if eventHandler.priority?

				# Wrap the event handler, and bind it to docpad
				docpad
					.off(eventName, eventHandler)
					.on(eventName, eventHandler)

		# Chain
		@


	###*
	# Remove Listeners
	# @private
	# @method removeListeners
	###
	removeListeners: -&gt;
		# Prepare
		pluginInstance = @
		docpad = @docpad
		events = docpad.getEvents()

		# Bind events
		eachr events, (eventName) -&gt;
			# Fetch the event handler
			eventHandler = pluginInstance[eventName]

			# Check it exists and is a function
			if typeChecker.isFunction(eventHandler)
				# Wrap the event handler, and unbind it from docpad
				docpad.off(eventName, eventHandler)

		# Chain
		@

	###*
	# Destructor. Calls removeListeners
	# @private
	# @method destroy
	###
	destroy: -&gt;
		@removeListeners()
		@


	###*
	# Is Enabled?
	# @method isEnabled
	# @return {Boolean}
	###
	isEnabled: -&gt;
		return @config.enabled isnt false


# ---------------------------------
# Export Plugin
module.exports = BasePlugin

    </pre>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
